<?php
/**
 * MageWorx
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the MageWorx EULA that is bundled with
 * this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.mageworx.com/LICENSE-1.0.html
 *
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@mageworx.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade the extension
 * to newer versions in the future. If you wish to customize the extension
 * for your needs please refer to http://www.mageworx.com/ for more information
 * or send an email to sales@mageworx.com
 *
 * @category   MageWorx
 * @package    MageWorx_DeliveryZone
 * @copyright  Copyright (c) 2009 MageWorx (http://www.mageworx.com/)
 * @license    http://www.mageworx.com/LICENSE-1.0.html
 */

/**
 * Delivery Zone extension
 *
 * @category   MageWorx
 * @package    MageWorx_DeliveryZone
 * @author     MageWorx Dev Team <dev@mageworx.com>
 */

class MageWorx_DeliveryZone_Model_Observer
{
    public function filterCategoriesByZone($observer)
    {
        $zone = Mage::getModel('deliveryzone/zone')->setType(MageWorx_DeliveryZone_Model_Zone::ZONE_TYPE_CATEGORY);
        $collection = $observer->getEvent()->getCategoryCollection();
        //var_dump(Mage::helper('deliveryzone')->getLocation());exit;
        if ($categoryIds = $zone->getByLocation(Mage::helper('deliveryzone')->getLocation())) {
            $collection->addFieldToFilter('entity_id', array('in' => $categoryIds));
        }
    }

    public function filterProductsByZone($observer)
    {
        if (Mage::registry('disable_products_filter', 0)) return;
        $zone = Mage::getModel('deliveryzone/zone')->setType(MageWorx_DeliveryZone_Model_Zone::ZONE_TYPE_CATEGORY);
        if ($productIds = $zone->getProductIdsByLocation(Mage::helper('deliveryzone')->getLocation())) {
            $collection = $observer->getEvent()->getCollection();
            $collection->addIdFilter($productIds);
        }
    }

    public function isCategoryAllowed($observer)
    {
        $zone = Mage::getModel('deliveryzone/zone')->setType(MageWorx_DeliveryZone_Model_Zone::ZONE_TYPE_CATEGORY);
        if ($categoryIds = $zone->getByLocation(Mage::helper('deliveryzone')->getLocation())) {
            $category = $observer->getEvent()->getCategory();
            if (!in_array($category->getId(), $categoryIds)) {
                $action = $observer->getEvent()->getControllerAction();
                Mage::getSingleton('core/session')->addError(Mage::helper('deliveryzone')->__('Category \'%s\' is unavailable', $category->getName()));
                $action->getResponse()->setRedirect(Mage::getBaseUrl());
            }
        }
    }

    public function isProductAllowed($observer)
    {
        $zone = Mage::getModel('deliveryzone/zone')->setType(MageWorx_DeliveryZone_Model_Zone::ZONE_TYPE_CATEGORY);
        if ($categoryIds = $zone->getByLocation(Mage::helper('deliveryzone')->getLocation())) {
            $product = $observer->getEvent()->getProduct();
            $productCategoryIds = $product->getCategoryIds();
            $diff = array_diff($productCategoryIds, $categoryIds);
            if (count($diff) == count($productCategoryIds)) {
                Mage::throwException(Mage::helper('deliveryzone')->__('Product \'%s\' is unavailable for purchase', $product->getName()));
            }
        }
    }

    public function resetShippingAddress($observer)
    {
        $quote = $observer->getEvent()->getQuote();
        $address = $quote->getShippingAddress();
        /*Modify by gold in 2011-6-30 disable the cookie location*/
        //$address->addData(Mage::helper('deliveryzone')->getLocation()->getData());
        $regionCollection = Mage::getModel('directory/region')->getResourceCollection()
                ->addCountryFilter($address->getCountryId())
                ->load();
        $data = array('country_id' => $address->getCountryId(), 'region_id' => (count($regionCollection) ? $address->getData('region_id') : ''));  
        Mage::helper('deliveryzone')->setLocation($data);
        $address->setCollectShippingRates(true);
    }
}